# -*- encoding=utf8 -*-
"""
RedFlag-TC0000103   （拍摄进入装饰页）下载字体功能
下载字体场景
"""
__author__ = "ldm"

import sys
import os

# 获取当前脚本所在目录.py
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取上级目录.air
parent_dir = os.path.dirname(current_dir)
# 获取上级目录air
grandparent_dir = os.path.dirname(parent_dir)
# 获取和air目录平级的conf、testcase目录路径
conf_dir = os.path.join(grandparent_dir, 'conf')
testcase_dir = os.path.join(grandparent_dir, 'testcase')
# 将conf、testcase目录添加到sys.path
sys.path.append(conf_dir)
sys.path.append(testcase_dir)

from PageFunction import CameraVideoShooter
from elements import *

auto_setup(__file__)
shooter = CameraVideoShooter()

log("--------------------------------下载字体场景--------------------------------------")
"""
拍摄视频进入装饰页
点击文字
封装的方法下载所有文字
"""

shooter.enter_photography_decoration_page()
shooter.click_element(decoration_page_text_function)

for loop in range(3):
    print(f"开始第 {loop + 1} 次循环")

    for i in range(12):
        element = decoration_page_text_function_font[i]
        if not element.exists():
            print(f"未找到索引为 {i} 的字体元素，退出循环")
            break

        download_img = element.offspring("cn.migu.video.clip.walle:id/walle_ugc_clip_typeface_item_download_img")

        if download_img.exists():
            shooter.click_element(element)
            result = shooter.wait_for_element_disappearance(download_img, timeout=30)
            if not result:
                print(f"索引 {i} 下载超时，跳过该字体")
                continue

        shooter.assert_element_not_exists(
            download_img,
            f"断言失败，索引 {i} 的下载按钮仍存在"
        )
        print(f"索引 {i} 的字体下载成功")

    if loop < 2:
        print("滑动屏幕")
        shooter.swipe_screen((0.5, 0.9), (0.5, 0.7))
        sleep(2)

# 关闭应用
stop_app(UGC_package_name)
